<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            let tree = {
                val: 1,
                left: {
                    val: 2,
                    left: {
                        val: 4,
                    },
                    right: {
                        val: 5,
                    },
                },
                right: {
                    val: 3,
                    left: {
                        val: 6,
                    },
                },
            }
            /* 思路：带有null的bfs然后 判断每一层是否对称，注意不能用null来join拼接字符串，因为null会清除，所以用做左右指针最好 */
            var isSymmetric = function (root) {
                if (!root) return true
                let res = []
                function bfs(root) {
                    let queue = [root]
                    while (queue.length) {
                        let len = queue.length
                        res.push([])
                        for (let i = 0; i < len; i++) {
                            let node = queue.shift()
                            if (node) {
                                res[res.length - 1].push(node.val)
                                queue.push(node.left)
                                queue.push(node.right)
                            } else {
                                res[res.length - 1].push(null)
                            }
                        }
                    }
                }
                bfs(root)

                return res.every((arr) => {
                    return isPali(arr)
                })

                function isPali(arr) {
                    //注意：null join以后会消失
                    let left = 0
                    let right = arr.length - 1
                    while (left <= right) {
                        if (arr[left] != arr[right]) {
                            return false
                        }
                        left++
                        right--
                    }
                    return true
                }
            }
            isSymmetric(tree)
        </script>
    </body>
</html>
